package com.goldenlight.fvms.manager.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.goldenlight.fvms.manager.entity.EmployFreeApply;
import com.goldenlight.fvms.manager.entity.ProductFreeApply;
import com.goldenlight.fvms.manager.excelentity.EmployCountExcelEntity;
import com.goldenlight.fvms.manager.filter.CollectTotalInfo;
import com.goldenlight.fvms.manager.service.EmployFreeApplyService;
import com.goldenlight.fvms.manager.service.ProductFreeApplyService;
import com.goldenlight.fvms.manager.utils.CommonUtils;
import com.goldenlight.fvms.manager.utils.ExcelUtils;
import com.goldenlight.fvms.manager.vo.DeleteVO;
import com.goldenlight.fvms.manager.vo.Request;
import com.goldenlight.fvms.manager.vo.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/publicFreeApply")
public class PublicApplyController {

        private Logger logger = LoggerFactory.getLogger(EmployFreeApplyController.class);
        @Autowired
        private EmployFreeApplyService employFreeApplyService;
        @Autowired
        private ProductFreeApplyService productFreeApplyService;
        @PostMapping("/totalSearch")
        public Response freeApplyTotal(HttpServletRequest request) {
            logger.info("request:{}", request);
           List<Map<String,Object>> retList= productFreeApplyService.qryProductCountInfo();
            List<Map<String,Object>> retList5= employFreeApplyService.qryEmployTotal();
            List<CollectTotalInfo> result = new ArrayList<>();
            result.addAll(CommonUtils.convertList(retList, CollectTotalInfo.class));
            result.addAll(CommonUtils.convertList(retList5, CollectTotalInfo.class));
            return Response.build(200, "success", result);
        }
    @DeleteMapping("delete")
    public Response delete(@RequestBody DeleteVO deleteVO) {
        logger.info("request:{}", deleteVO);
        int delete = 0;
        if(deleteVO.getSelectType()==null){
            delete=  employFreeApplyService.delete(deleteVO.getBuildVersion());
        }else {
            delete=productFreeApplyService.delete(deleteVO.getBuildVersion());
        }
        return Response.build(200, "success", delete);
    }
    @ApiOperation(value = "根据productFreeApply导出excel", notes = "导出excel")
        @PostMapping("/exportExcelCount")
        public void exportExcelCount(HttpServletRequest request,HttpServletResponse resp) {
        logger.info("exportExcel:{}");
        Response response = this.freeApplyTotal(request);
        List<CollectTotalInfo> list= (List<CollectTotalInfo>) response.getData();

        ExcelUtils.exportExcel(list, "汇总信息", "sheet1",
                CollectTotalInfo.class, "汇总信息导出.xlsx", true, resp);
     }


}
